﻿using System;
using Shuba.Model;

namespace Shuba.Business
{
    public class Authorization
    {
        private Data.User _privilege;

        public Authorization()
        {
            _privilege = new Data.User();
        }

        public string GetUserPrivilege(int userId, int menuId, string AppCode)
        {
            return _privilege.GetUserPrivilege(userId, menuId, AppCode);
        }

        //public string GetUserPrivilege(int userId, int menuId, string AppCode, string UserType)
        //{
        //    return _privilege.GetUserPrivilege(userId, menuId, AppCode, UserType);
        //}
    }

    public class Authentication
    {
        public Authentication()
        { 
        }

        /*
        public bool ValidateUser(string nip, string AppCode)
        {
            User _user = new User();
            UserInfo _userInfo;
            bool status = false;

            _userInfo = _user.Get(nip, AppCode);
            if (_userInfo.Nip != null)
            {
                status = true;
            }
            else
            {
                status = false;
            }

            return status;
        }
        */

        public int ValidateUser(string nip, string AppCode)
        {
            User _user = new User();
            UserInfo _userInfo;
            int status = 1; //jika user valid
            bool isCekAlternate = false;
            string AlternateFor = "";

            _userInfo = _user.Get(nip, AppCode);
            AlternateFor = _user.GetUserAlternate(nip, AppCode); //Cek Apakah user sedang menjadi Alternate

            if (_userInfo.Nip != null)
            {
                if (AlternateFor != "")
                {
                    status = 2; //jika user alternate valid
                    isCekAlternate = true;
                    if (_user.CekExistingAlternate(_userInfo.BranchId, AlternateFor, nip))
                    {
                        status = 4;
                    }
                    else status = 1;
                }
                if (status == 1 && !isCekAlternate)
                {
                    if (AlternateFor == "") AlternateFor = _userInfo.UserType;
                    if (_user.CekExistingAlternate(_userInfo.BranchId, AlternateFor, nip))
                    {
                        status = 5;
                    }
                    else status = 1;
                }
            }
            else if (AlternateFor != "")
            {
                status = 2; //jika user alternate valid
                
                /*
                 Cek apakah ada alternate lain yang active
                 Sebenarnya logic ini sudah di handle pada saat setting alternate.
                 Checking ini ditambahkan just in case data dirubah backdoor
                */
                if (_user.CekExistingAlternate(_userInfo.BranchId, AlternateFor, nip)) status = 6; 

            }
            else if (AlternateFor =="")
            {
                status = 7; //jika user tidak punya hak ke SPAWEB
            }
            else
            {
                status = 3; //jika user & user alternate tidak punya hak ke SPAWEB
            }

            return status;
        }
    }
}